import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    private Queue<Integer> q1=new LinkedList<>();
    private Queue<Integer> q2=new LinkedList<>();
    public MyStack() {
    }

    public void push(int x) {
        if(!q1.isEmpty()){
            q1.offer(x);
        }else if(!q2.isEmpty()){
            q2.offer(x);
        }else{
            q1.offer(x);
        }
    }
    
    public int pop() {
        if(empty()){
            return -1;
        }
        int cur=0;
        
        if(!q1.isEmpty()){
            while(!q1.isEmpty()){
                cur=q1.poll();
                if(!q1.isEmpty()){
                    q2.offer(cur);
                }
            }
        }else{
            while(!q2.isEmpty()){
                cur=q2.poll();
                if(!q2.isEmpty()){
                    q1.offer(cur);
                }
            }
        }
        return cur;
    }
    
    public int top() {
        if(empty()){
            return -1;
        }
        int cur=0;
        if(!q1.isEmpty()){
            while(!q1.isEmpty()){
                cur=q1.poll();
                q2.offer(cur);
            }
        }else{
            while(!q2.isEmpty()){
                cur=q2.poll();
                q1.offer(cur);
            }
        }
        return cur;
    }
    
    public boolean empty() {
        return q1.size()==0&&q2.size()==0;
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */